package com.study.yangfei.mystudy.activity;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import com.study.yangfei.mystudy.R;

import java.util.ArrayList;

public class AlogGreedyActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alog_greedy);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        greedyalog();
    }

    class bagObj{
        public int weight;
        public int value;
        public float wv;
        public boolean isSelected;
        public int index;

        public bagObj(int v,int w,int index){
            weight = w;
            value = v;
            isSelected = false;
            wv = (float)v/(float)w;
            this.index = index;
        }
    }

    class Bag{
        public int maxCap;
        public int capSum;
        public int valueSum;
        ArrayList<bagObj> mObjList = new ArrayList<>();

        public Bag(int mc){
            maxCap = mc;
        }
    }

    ArrayList<bagObj> mObjList = new ArrayList<>();
    ArrayList<bagObj> mObjListOdered = new ArrayList<>();
    Bag mBag;
    public void greedyalog(){
        int index = 1;
        mObjList.add(new bagObj(10,35,index++));
        mObjList.add(new bagObj(40,30,index++));
        mObjList.add(new bagObj(30,60,index++));
        mObjList.add(new bagObj(50,50,index++));
        mObjList.add(new bagObj(35,40,index++));
        mObjList.add(new bagObj(40,10,index++));
        mObjList.add(new bagObj(30,25,index++));

        mBag = new Bag(150);

        for(bagObj bo:mObjList){
            if(mObjListOdered.size() == 0){
                mObjListOdered.add(bo);
            }else{
                int i = 0;
                for(i = 0;i<mObjListOdered.size();i++){
                    bagObj boOder = mObjListOdered.get(i);
                    if(boOder.wv<bo.wv){
                        mObjListOdered.add(i,bo);
                        break;
                    }
                }
                if(i >= mObjListOdered.size()){
                    mObjListOdered.add(bo);
                }
            }
        }

        Log.d("greedyLog","---------------mObjListOdered-----------------------");
        for(bagObj bo:mObjListOdered){
            Log.d("greedyLog","object wv = " + bo.wv +" object value = " + bo.value + " object weight = "+bo.weight+ " object index = "+bo.index);
        }
        Log.d("greedyLog","---------------mObjListOdered-----------------------");

        for(bagObj boOder:mObjListOdered){
            if((mBag.capSum+boOder.weight)<=mBag.maxCap){
                mBag.mObjList.add(boOder);
                mBag.capSum += boOder.weight;
                mBag.valueSum += boOder.value;
            }
        }

        Log.d("greedyLog","bag sum weight = "+mBag.capSum + " bag sum value = "+ mBag.valueSum);
        for(bagObj bo:mBag.mObjList){
            Log.d("greedyLog","object value = " + bo.value + " object weight = "+bo.weight+ " object index = "+bo.index);
        }

    }

}
